package com.leiou.stu_list;

import com.google.gson.Gson;
import com.leiou.pojo.Student;
import com.leiou.utils.CommonResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/*
 *蒋碧鹏
 *
 */
@WebServlet("/queryStudent")
public class QueryStudentHandler extends HttpServlet {
    // 查询学生信息
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String keyword = req.getParameter("keywords");
        StulistDao stulistDao = new StulistDaoImpl();
        List<Student> findStudentByKeyword = stulistDao.findStudentByKeyword(keyword);
        Gson gson = new Gson();

        if (findStudentByKeyword.size() > 0) {
            CommonResult<List<Student>> success = CommonResult.success(findStudentByKeyword);
            String json = gson.toJson(success);
            resp.getWriter().write(json);
        } else {
            CommonResult<List<Student>> fail = CommonResult.fail(404, "没有找到相关学生");
            String json = gson.toJson(fail);
            resp.getWriter().write(json);
        }
    }
}
